﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using UnityEditor.Utils;
    using UnityEditorInternal;
    using UnityEngine;

    internal class PostprocessBuildPlayer
    {
        internal const string StreamingAssets = "Assets/StreamingAssets";

        internal static string ExecuteSystemProcess(string command, string args, string workingdir)
        {
            ProcessStartInfo si = new ProcessStartInfo {
                FileName = command,
                Arguments = args,
                WorkingDirectory = workingdir,
                CreateNoWindow = true
            };
            Program program = new Program(si);
            program.Start();
            while (!program.WaitForExit(100))
            {
            }
            string standardOutputAsString = program.GetStandardOutputAsString();
            program.Dispose();
            return standardOutputAsString;
        }

        internal static string GenerateBundleIdentifier(string companyName, string productName)
        {
            return ("unity." + companyName + "." + productName);
        }

        public static string GetExtensionForBuildTarget(BuildTarget target)
        {
            switch (target)
            {
                case BuildTarget.StandaloneOSXIntel:
                    return "app";

                case BuildTarget.StandaloneWindows:
                case BuildTarget.StandaloneGLESEmu:
                case BuildTarget.StandaloneWindows64:
                    return "exe";

                case BuildTarget.WebPlayer:
                case BuildTarget.WebPlayerStreamed:
                    return string.Empty;

                case BuildTarget.Android:
                    return "apk";

                case BuildTarget.StandaloneLinux:
                case BuildTarget.StandaloneLinux64:
                    return string.Empty;

                case BuildTarget.FlashPlayer:
                    return "swf";

                case BuildTarget.MetroPlayerX86:
                case BuildTarget.MetroPlayerX64:
                case BuildTarget.MetroPlayerARM:
                    return ((EditorUserBuildSettings.metroBuildType != MetroBuildType.AppX) ? string.Empty : "appx");

                case BuildTarget.StandaloneLinuxUniversal:
                    return "x86";
            }
            return string.Empty;
        }

        internal static void InstallPlugins(string pluginFolder, BuildTarget target)
        {
            string str = "Assets/Plugins";
            List<string> list = new List<string>();
            bool flag = false;
            bool copyDirectories = target == BuildTarget.StandaloneOSXIntel;
            string extension = string.Empty;
            if (target == BuildTarget.StandaloneOSXIntel)
            {
                extension = ".bundle";
                list.Add(string.Empty);
            }
            else if (target == BuildTarget.StandaloneWindows)
            {
                extension = ".dll";
                if (Directory.Exists(Path.Combine(str, PostProcessStandalonePlayer.subDir32Bit)))
                {
                    list.Add(PostProcessStandalonePlayer.subDir32Bit);
                }
                else
                {
                    list.Add(string.Empty);
                }
            }
            else if (target == BuildTarget.StandaloneWindows64)
            {
                extension = ".dll";
                if (Directory.Exists(Path.Combine(str, PostProcessStandalonePlayer.subDir64Bit)))
                {
                    list.Add(PostProcessStandalonePlayer.subDir64Bit);
                }
                else
                {
                    list.Add(string.Empty);
                }
            }
            else if (target == BuildTarget.StandaloneGLESEmu)
            {
                extension = ".dll";
                list.Add(string.Empty);
            }
            else if (target == BuildTarget.StandaloneLinux)
            {
                extension = ".so";
                if (Directory.Exists(Path.Combine(str, PostProcessStandalonePlayer.subDir32Bit)))
                {
                    list.Add(PostProcessStandalonePlayer.subDir32Bit);
                }
                else
                {
                    list.Add(string.Empty);
                }
            }
            else if (target == BuildTarget.StandaloneLinux64)
            {
                extension = ".so";
                if (Directory.Exists(Path.Combine(str, PostProcessStandalonePlayer.subDir64Bit)))
                {
                    list.Add(PostProcessStandalonePlayer.subDir64Bit);
                }
                else
                {
                    list.Add(string.Empty);
                }
            }
            else if (target == BuildTarget.StandaloneLinuxUniversal)
            {
                extension = ".so";
                list.Add(PostProcessStandalonePlayer.subDir32Bit);
                list.Add(PostProcessStandalonePlayer.subDir64Bit);
                flag = true;
            }
            else if (target == BuildTarget.PS3)
            {
                extension = ".sprx";
                list.Add(string.Empty);
            }
            else if (target == BuildTarget.Android)
            {
                extension = ".so";
                list.Add("Android");
            }
            foreach (string str3 in list)
            {
                if (flag)
                {
                    InstallPluginsByExtension(Path.Combine(str, str3), extension, Path.Combine(pluginFolder, str3), copyDirectories);
                }
                else
                {
                    InstallPluginsByExtension(Path.Combine(str, str3), extension, pluginFolder, copyDirectories);
                }
            }
        }

        internal static bool InstallPluginsByExtension(string pluginSourceFolder, string extension, string pluginFolder, bool copyDirectories)
        {
            bool flag = false;
            if (Directory.Exists(pluginSourceFolder))
            {
                foreach (string str in Directory.GetFileSystemEntries(pluginSourceFolder))
                {
                    if ((string.Compare(Path.GetExtension(str), extension, true) == 0) || (string.Compare(Path.GetFileName(str), extension, true) == 0))
                    {
                        if (!Directory.Exists(pluginFolder))
                        {
                            Directory.CreateDirectory(pluginFolder);
                        }
                        string target = Path.Combine(pluginFolder, Path.GetFileName(str));
                        if (copyDirectories)
                        {
                            FileUtil.CopyDirectoryRecursive(str, target);
                        }
                        else
                        {
                            FileUtil.UnityFileCopy(str, target);
                        }
                        flag = true;
                    }
                }
            }
            return flag;
        }

        public static void Launch(BuildTarget target, string path)
        {
            if (target == BuildTarget.iPhone)
            {
                PostProcessiPhonePlayer.Launch(target, path);
            }
            else
            {
                if (target != BuildTarget.NaCl)
                {
                    throw new UnityException(string.Format("Launching {0} build target via mono is not supported", target));
                }
                PostProcessNaclPlayer.Launch(target, path);
            }
        }

        public static void Postprocess(BuildTarget target, string installPath, string companyName, string productName, int width, int height, string downloadWebplayerUrl, string manualDownloadWebplayerUrl, BuildOptions options, RuntimeClassRegistry usedClassRegistry)
        {
            string stagingArea = "Temp/StagingArea";
            string stagingAreaData = "Temp/StagingArea/Data";
            string stagingAreaDataManaged = "Temp/StagingArea/Data/Managed";
            string playbackEngineDirectory = BuildPipeline.GetPlaybackEngineDirectory(target, options);
            bool flag = ((options & BuildOptions.InstallInBuildFolder) != BuildOptions.None) && SupportsInstallInBuildFolder(target);
            if ((installPath == string.Empty) && !flag)
            {
                throw new System.Exception(installPath + " must not be an empty string");
            }
            switch (target)
            {
                case BuildTarget.StandaloneOSXIntel:
                    PostProcessStandalonePlayer.PostProcessStandaloneOSXPlayer(target, options, installPath, stagingAreaData, stagingArea, playbackEngineDirectory, companyName, productName);
                    return;

                case BuildTarget.StandaloneWindows:
                case BuildTarget.StandaloneGLESEmu:
                case BuildTarget.StandaloneWindows64:
                    PostProcessStandalonePlayer.PostProcessStandaloneWindowsPlayer(target, options, installPath, stagingAreaData, stagingArea, playbackEngineDirectory);
                    return;

                case BuildTarget.WebPlayer:
                case BuildTarget.WebPlayerStreamed:
                    PostProcessWebPlayer.PostProcess(options, installPath, downloadWebplayerUrl, width, height);
                    return;

                case BuildTarget.Wii:
                    PostProcessWiiPlayer.PostProcess(target, options, installPath, stagingAreaData, stagingArea, productName, playbackEngineDirectory);
                    return;

                case BuildTarget.iPhone:
                    PostProcessiPhonePlayer.PostProcess(target, stagingAreaData, stagingArea, stagingAreaDataManaged, playbackEngineDirectory, installPath, companyName, productName, options, usedClassRegistry);
                    return;

                case BuildTarget.PS3:
                    PostProcessPS3Player.PostProcess(target, options, installPath, stagingAreaData, stagingArea, playbackEngineDirectory, stagingAreaDataManaged, usedClassRegistry);
                    return;

                case BuildTarget.XBOX360:
                    PostProcessXbox360Player.PostProcess(target, options, installPath, stagingAreaData, stagingArea, playbackEngineDirectory, stagingAreaDataManaged);
                    return;

                case BuildTarget.Android:
                    PostProcessAndroidPlayer.PostProcess(target, stagingAreaData, stagingArea, playbackEngineDirectory, installPath, companyName, productName, options);
                    return;

                case BuildTarget.NaCl:
                    PostProcessNaclPlayer.PostProcess(options, installPath, downloadWebplayerUrl, width, height);
                    return;

                case BuildTarget.StandaloneLinux:
                case BuildTarget.StandaloneLinux64:
                    PostProcessStandalonePlayer.PostProcessStandaloneLinuxPlayer(target, options, installPath, stagingAreaData, stagingArea, playbackEngineDirectory);
                    return;

                case BuildTarget.FlashPlayer:
                {
                    PostProcessFlashPlayerOptions options2 = new PostProcessFlashPlayerOptions {
                        Target = target,
                        StagingAreaData = stagingAreaData,
                        StagingArea = stagingArea,
                        StagingAreaDataManaged = stagingAreaDataManaged,
                        PlayerPackage = playbackEngineDirectory,
                        InstallPath = installPath,
                        CompanyName = companyName,
                        ProductName = productName,
                        Options = options,
                        UsedClassRegistry = usedClassRegistry,
                        Width = width,
                        Height = height
                    };
                    PostProcessFlashPlayer.PostProcess(options2);
                    return;
                }
                case BuildTarget.MetroPlayerX86:
                case BuildTarget.MetroPlayerX64:
                case BuildTarget.MetroPlayerARM:
                    PostProcessMetroPlayer.PostProcess(target, options, installPath, stagingAreaData, stagingArea, playbackEngineDirectory);
                    return;

                case BuildTarget.StandaloneLinuxUniversal:
                    PostProcessStandalonePlayer.PostProcessUniversalLinuxPlayer(target, options, installPath, stagingAreaData, stagingArea, playbackEngineDirectory);
                    return;
            }
            throw new UnityException("Build target not supported");
        }

        internal static void PostprocessStandalonePlayerInBuildFolder(string stagingAreaData)
        {
            string path = null;
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                path = Unsupported.GetBaseUnityDeveloperFolder() + "/build/WindowsStandalonePlayer/Data";
            }
            else
            {
                if (Application.platform != RuntimePlatform.OSXEditor)
                {
                    throw new System.Exception("Unsupported");
                }
                path = Unsupported.GetBaseUnityDeveloperFolder() + "/build/MacStandalonePlayer/Data";
            }
            FileUtil.DeleteFileOrDirectory(path);
            FileUtil.MoveFileOrDirectory(stagingAreaData, path);
        }

        public static bool SupportsInstallInBuildFolder(BuildTarget target)
        {
            switch (target)
            {
                case BuildTarget.StandaloneOSXIntel:
                case BuildTarget.StandaloneWindows:
                case BuildTarget.iPhone:
                case BuildTarget.PS3:
                case BuildTarget.XBOX360:
                case BuildTarget.Android:
                case BuildTarget.StandaloneGLESEmu:
                case BuildTarget.StandaloneWindows64:
                case BuildTarget.MetroPlayerX86:
                case BuildTarget.MetroPlayerX64:
                case BuildTarget.MetroPlayerARM:
                    return true;
            }
            return false;
        }
    }
}

